import {defineStore} from "pinia";
import axios from "axios";
import {message} from "ant-design-vue";

export const useUserStore = defineStore('user', {
    state: () => ({
        userInfo: {},
        loading: false,
        error: null,
    }),

    actions: {
        updateUserInfo(userInfo) {
            this.userInfo = userInfo;
        },
        async login(account, password) {
            this.loading = true;
            this.error = null;
            try {
                const response = await axios.post('/api/user/login', {
                    account,
                    password
                });
                if (response.data.code === 200) {
                    console.log(response.data.data)
                    this.userInfo = response.data.data;
                    localStorage.setItem('token', JSON.stringify(response.data.data.token));
                    return true;
                }
            } catch (error) {
                this.error = error.response ? error.response.data.message : '登录失败';
            } finally {
                this.loading = false;
            }
            return false;
        },
        async register(account, username, email, password) {
            this.loading = true;
            this.error = null;
            try {
                const response = await axios.post('/api/user/register', {
                    account,
                    username,
                    email,
                    password
                });
                console.log(response.data)
                if (response.data.code === 200) {
                    message.success("注册成功");
                    return true;
                } else if (response.data.code === 401) {
                    message.error("账号重复，注册失败");
                } else if (response.data.code === 402) {
                    message.error("邮箱重复, 注册失败");
                } else {
                    message.error("系统故障,请重试");
                }
            } catch (error) {
                this.error = error.response ? error.response.data.message : '注册失败';
            } finally {
                this.loading = false;
            }
            return false;
        },
        async saveUserInfo(userInfo, userId) {
            this.loading = true;
            this.error = null;
            try {
                const requestData = {...userInfo, userId}
                console.log(requestData);
                const response = await axios.post('/api/user/saveUserInfo', requestData);
                if (response.data.code === 200) {
                    message.success("保存成功");
                    this.updateUserInfo(requestData);
                    return true;
                } else {
                    message.error("保存失败");
                }
            } catch (error) {
                this.error = error.response ? error.response.data.message : '保存失败';
            } finally {
                this.loading = false;
            }
            return false;
        },
        logout() {
            this.userInfo = {};
            localStorage.removeItem('token');
            this.error = null;
            this.loading = false;
        }
    }
})